<?php 
class ControllerToolRedirect301tool extends Controller { 
	private $error = array();
	
	public function index() {
		$this->load->language('tool/redirect301tool');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('tool/redirect301tool');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
					
							if( $this->model_tool_redirect301tool->saveUrls($this->request->post['urls'])){
								$this->session->data['success'] = $this->language->get('text_success');
								$this->redirect($this->url->link('tool/redirect301tool', 'token=' . $this->session->data['token'], 'SSL'));
							}else{
								$this->session->data['error_warning'] = $this->language->get('text_nochange');
								$this->redirect($this->url->link('tool/redirect301tool', 'token=' . $this->session->data['token'], 'SSL'));	
								
							}
						}
			
		

		if (!empty($this->session->data['redirect301tool_error']['errstr'])) {
			$this->error['warning'] = $this->session->data['redirect301tool_error']['errstr'];
			if (!empty($this->session->data['redirect301tool_nochange'])) {
				$this->error['warning'] .= '<br />'.$this->language->get( 'text_nochange' );
			}
			$this->error['warning'] .= '<br />'.$this->language->get( 'text_log_details' );
		}
		unset($this->session->data['redirect301tool_error']);
		unset($this->session->data['redirect301tool_nochange']);

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['entry_restore'] = $this->language->get('entry_restore');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_add_url'] = $this->language->get('button_add_url');
		$this->data['button_remove'] = $this->language->get('button_remove');
		
		$this->data['route_url'] = $this->language->get('route_url');
		$this->data['short_url'] = $this->language->get('short_url');
		
        $this->data['urls']    =  array(); //$this->model_tool_redirect301tool->getUrls();
		//$this->data['urls'] = $this->model_tool_redirect301tool->getUrls();
		$urls =  $this->model_tool_redirect301tool->getUrls();
		
		foreach ($urls as $url) {
 		    $que = $url['query'];
			if(strpos($url['query'],'=') ) $que = substr($url['query'],strpos($url['query'],'=')+1,strlen($url['query']));
			
			
			$this->data['urls'][] = array(
				'query'        =>  $que ,
				'keyword'      =>  $url["keyword"]
			);
		}
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => FALSE
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('tool/redirect301tool', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);
		
		$this->data['action'] = $this->url->link('tool/redirect301tool', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->template = 'tool/redirect301tool.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
		$this->response->setOutput($this->render());
	}




	private function validate() {
		if (!$this->user->hasPermission('modify', 'tool/redirect301tool')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>